#pragma once

#include<cassert>
#include<cstdlib>
#include<cstdarg>
#include<cstdio>
#include<ctime>


#define DEBUG 0
#define NOTICE 1
#define WARNNING 2
#define FATAL 3

const char* log_level[] = {"DEBUG", "NOTICE", "WARNNING", "FATAL"};

//logMessage(DEBUG, "%d", 10);
void logMessage(int level, const char* format, ...)
{
    assert(level >= DEBUG);
    assert(level <= FATAL);

    char* name = getenv("USER");
    char logInfo[1024];

    va_list ap;//定义可以访问可变参数部分的变量 -- char*
    va_start(ap, format);//使ap指向可变参数部分
    vsnprintf(logInfo, sizeof(logInfo) - 1, format, ap);
    va_end(ap);//ap = NULL

    FILE* out = (level == FATAL) ? stderr : stdout;

    fprintf(out, "%s | %u | %s | %s\n", \
            log_level[level], \
            (unsigned int)time(nullptr), \
            name == nullptr ? "unknow" : name, \
            logInfo);

}